package com.le.edu.controller;

import cn.hutool.core.util.ObjectUtil;
import com.le.edu.domain.entity.UploadResult;
import com.le.edu.resp.Result;
import com.le.edu.service.OssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Le
 * @create 2024-04-04 12:35
 */
@Tag(name = "文件相关接口")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/oss")
public class OssController {
    private final OssService ossService;

    @Operation(summary = "上传图片接口")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<UploadResult> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return Result.fail("上传文件不能为空");
        }
        UploadResult result = ossService.uploadFile(file);
        return Result.ok(result);
    }
}
